import axios from 'axios'
import { Notify, Toast } from 'vant';
import store from '@/store'
import { getToken } from '@/utils/token'

// create an axios instance
const service = axios.create({
  timeout: 5000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    //cookie 和 header 都可以，这里是两者我都加了
    config.headers['Authorization'] = getToken()
    return config
  },
  error => {
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const {data} = response;
    if(data.code == 101){
      Toast(data.errMsg)
      return Promise.reject(data)
    }
    return data;
  },
  error => {
    Notify('请求报错!')
    return Promise.reject(error)
  }
)

export default service
